#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL cnt=0;
LL fa[maxn];
LL find(LL x)
{
    if(fa[x]!=x) return fa[x]=find(fa[x]);
    return fa[x];
}
struct Edge{
    LL u,v,w;
    bool operator<(const Edge& b)
    {
        return this->w<b.w;
    }
}edges[maxn];

int main(void){
    LL n,m;n=read();m=read();
    for(int i=1;i<=n;i++) fa[i]=i;
    for(int i=1;i<=m;i++)
    {
        int _u,_v,_w;_u=read();_v=read();_w=read();
        edges[++cnt].u=_u;
        edges[cnt].v=_v;
        edges[cnt].w=_w;
    }
    sort(edges+1,edges+1+cnt);
    LL ans=0;
    LL cnt=0;
    for(int i=1;i<=m;i++)
    {
        if(cnt==n)
        {
            if(edges[i].w>0) ans+=edges[i].w;
            continue;
        }
        LL u=edges[i].u;LL v=edges[i].v;
        LL fu=find(u);LL fv=find(v);
        if(fu==fv)
        {
            if(edges[i].w>0)
            {
                ans+=edges[i].w;
            }
            continue;
        }
        fa[fu]=fv;
        cnt++;
    }
    printf("%lld\n",ans);
   	return 0;
}
